import opfunu


class CEC():
    def __init__(self, year=2017, test_name='F1', dim=30):
        '''
            适应度函数及维度dim的选择
            cec函数名字格式：函数名+年份，比如要选择2022的F1函数，func_num = 'F1'+'2022'
            cec2005：F1-F25, 可选 dim = 10, 30, 50
            cec2008：F1-F7,  可选 2 <= dim <= 1000
            cec2010：F1-F20, 可选 100 <= dim <= 1000
            cec2013：F1-F28, 可选 dim = 2, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
            cec2014：F1-F30, 可选 dim = 10, 20, 30, 50, 100
            cec2015：F1-F15, 可选 dim = 10, 30
            cec2017：F1-F29, 可选 dim = 2, 10, 20, 30, 50, 100
            cec2019：F1-F10, 可选 dim: F1=9,F2=16,F3=18,其他=10
            cec2020：F1-F10, 可选 dim = 2, 5, 10, 15, 20, 30, 50, 100
            cec2021：F1-F10, 可选 dim = 2, 10, 20
            cec2022：F1-F12, 可选 dim = 2, 10, 20
        '''
        self.year = str(year)
        self.fun_num = test_name
        self.dim = dim
        self.func_num = test_name+str(year)

    def fobj(self, x):
        funcs = opfunu.get_functions_by_classname(self.func_num)
        func = funcs[0](ndim=self.dim)
        F = func.evaluate(x)
        return F
